<?php 
namespace app\supermanager\controller;
use think\Request;
/**
 * 营地管理类
 */
class Camp extends \think\Controller
{
	use \app\common\traits\controller\TraitController;
    use \app\common\traits\controller\TraitUpload;
	protected function init(){
		if (!model('Managers')->checkLogin()) {
            $this->redirect('@manager/login');
            exit;
        }
		$this->model = model('Camps');
		$this->list_route = url('/camps');
	}
	
	/**
	 * 资源数据列表
	 */
	public function index(){
		//分公司
		$filiales = model('filiales')->column('name','id');
		$this->assign('filiales',$filiales);
		//获得营地信息
		$campName = $this->model->getCampName();
		$this->assign('campName',$campName);
		$list = $this->model->getCaravansLists(input('get.'));
	    return view('camp/index',['list'=>$list]);
	}
	
	
	/**
	 * 更改资源信息表单
	 */
	public function edit($id){
		//营地类型
		$types = model('CampTypes')->column('name','id');
		$this->assign('types',$types);
		//营地星级
		$grades = model('CampGrades')->column('name','id');
		//营地所在热门城市
		$city = model('Camps')->getCity();
		$this->assign('grades',$grades);
		$list = $this->model->where(['id'=>$id])->find();
	    return view('camp/edit',['list'=>$list,'city'=>$city]);
	}
	
	/**
	 * 更改资源--->通过审核
	 * @param -99 审核未通过
	 * @param 1 审核通过
	 */
	public function update(Request $request,$id){
		if(!is_int($request->post('travel_money_base/d')) || $request->post('travel_money_base') <= 0){
			$this->error('营地旅行币基数填写格式错误或不能为空');
			return false;
		}
		if(empty($request->post('area'))){
			$this->error('所在热门城市不能为空');
			return false;
		}
		if(!is_int($request->post('rent_money_base/d')) || $request->post('rent_money_base') <= 0){
			$this->error('营地单日出租基数填写格式错误或不能为空');
			return false;
		}
		if ($request->post('old_rent_money_base') != $request->post('rent_money_base')) {
			queue('common/JobPay@changeRentMoneyBase', $id, $delay = 0, $queue = 'order');
		}
		//营地封面
        if (input('post.cover')) {
        	$array_info['cover'] = input('post.cover'); 
        }
		//营业执照
		if ($license = input('post.license')) {
			$array_info['license'] = $license;
        }
		$array_info['status'] = 1; 
		$array_info['country'] = input('post.addressCountry');
		$array_info['province'] = input('post.addressProvince');
		$array_info['city'] = input('post.addressCity');
	    if($this->model->updateInfo(array_merge($request->post(['id'=>$id]),$array_info))){
			$this->success('更新成功',$this->list_route);
		}
		$this->error('更新失败');
	}
	
	
	/**
	 * 未通过审核
	 */
	public function notPass(Request $request,$id){
		if(!is_int($request->post('travel_money_base/d')) || $request->post('travel_money_base') <= 0){
			$this->error('营地旅行币基数填写格式错误或不能为空');
			return false;
		}
		if(empty($request->post('area'))){
			$this->error('所在热门城市不能为空');
			return false;
		}
		if(!is_int($request->post('rent_money_base/d')) || $request->post('rent_money_base') <= 0){
			$this->error('营地单日出租基数填写格式错误或不能为空');
			return false;
		}
		//营地封面
        if (input('post.cover')) {
        	$array_info['cover'] = input('post.cover'); 
        }
		//营业执照
		if ($license = input('post.license')) {
			$array_info['license'] = $license;
        }
		$array_info['status'] = -99; 
		$array_info['country'] = input('post.addressCountry');
		$array_info['province'] = input('post.addressProvince');
		$array_info['city'] = input('post.addressCity');
	    if($this->model->updateInfo(array_merge($request->post(['id'=>$id]),$array_info))){
			$this->success('更新成功',$this->list_route);
		}
		$this->error('更新失败');
	}
	
	/**
	 * 删除资源
	 */
	public function delete($id){
		if($this->model->delInfo($id)){
			$this->success('删除成功',$this->list_route);
		}
		$this->error('删除失败');
	}
	
	

	//地区插件
    public function getCountry()
    {
        $country = db('area')->where('type',0)->field(['area_code','parent_code','area_name','type'])->select();;
        return json($country);
        exit;
    }
    public function getProvince()
    {
        $addressProvince = input('addressCountry');
        $province = db('area')->where('type',1)->where('parent_code',$addressProvince)->field(['area_code','parent_code','area_name','type'])->select();
        return  json($province);
    }
    public function getCity()
    {
        $city = input('addressProvince');
        $city = db('area')->where('type',2)->where('parent_code',$city)->field(['area_code','parent_code','area_name','type'])->select();
        return json($city);
    }
	
	
	
	
	
	
}
 ?>
 











